package com.ihd.ihardware.view.tzc.me.view;

import android.databinding.ObservableArrayList;
import android.os.Bundle;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityFansBinding;
import com.ihd.ihardware.pojo.RecommendRes;
import com.ihd.ihardware.view.tzc.me.viewmodel.FansViewModel;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.rxbus.RxBus;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class FansActivity extends BaseActivity<ActivityFansBinding, FansViewModel> {

    private Disposable msg;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();
        binding.mtitlebar.setTitle("我的粉丝");
        binding.mtitlebar.setLeftBack(this);

        setRx(AppConstans.FANS, new BaseConsumer<RecommendRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(RecommendRes mLiveData) {
                viewModel.setFansList(mLiveData.getData());
                if (mLiveData.getData().size() == 0)
                    binding.emptyRL.setVisibility(View.VISIBLE);
                else {
                    binding.emptyRL.setVisibility(View.GONE);
                }
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        msg = RxBus.getInstance().tObservable(AppConstans.FCONCERN, Bundle.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bundle>() {
                    @Override
                    public void accept(Bundle b) throws Exception {
                        if (b.getInt("key") == 0) {
                            ObservableArrayList<RecommendRes.DataBean> sds = (ObservableArrayList<RecommendRes.DataBean>) viewModel.mConcernedAdapter.getItems();
                            for (int i = 0; i < sds.size(); i++) {
                                if (sds.get(i).getUserId().equals(b.getString("value"))) {
                                    if (b.getInt("type") == 0) {
                                        sds.get(i).setConcern(false);
                                    } else {
                                        sds.get(i).setConcern(true);
                                    }
                                    viewModel.mConcernedAdapter.notifyItemChanged(i);
                                }
                            }
                        } else
                            ToastUtils.showShort(FansActivity.this, b.getString("value"));
                    }
                });

        binding.fansRV.setAdapter(viewModel.mConcernedAdapter);
        viewModel.getFansList();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_fans;
    }

    @Override
    protected Class getViewModelClass() {
        return FansViewModel.class;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (msg != null && !msg.isDisposed())
            msg.dispose();
    }
}
